﻿// ***********************************************************************
// Assembly         : XW.Core
// Author           : rx.tang
// Created          : 10-23-2017
//
// Last Modified By : rx.tang
// Last Modified On : 10-23-2017
// ***********************************************************************
// <copyright file="BaseServiceUtils.cs" company="瑞新">
//     Copyright © 瑞新 2017
// </copyright>
// <summary></summary>
// ***********************************************************************

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XW.Utils;
using System.Web;
using XW.Core.Infrastructure;
using Microsoft.AspNetCore.Http;

namespace XW.Core
{
    public partial class BaseService : IDisposable
    {
        /// <summary>
        /// 缓存前缀
        /// </summary>
        public virtual string CacheKeyPre
        {
            get { return ""; }
        }
        /// <summary>
        /// Gets the columns.
        /// </summary>
        /// <param name="obj">The object.</param>
        /// <returns>IDictionary&lt;System.String, System.Object&gt;.</returns>
        public IDictionary<string, object> GetColumns(object obj)
        {
            IDictionary<string, object> columns = obj is IDictionary<string, object> ? obj as IDictionary<string, object> : obj.GetPropertiesDic();

            return columns;
        }

        public ParamBase GetParamFromForm(HttpRequest request, string id = "Id", Func<Dictionary<string, object>, bool> checkIsInsert = null)
        {
            return new ParamBase(request, id, checkIsInsert);
        }
        /// <summary>
        /// 清除缓存
        /// </summary>
        public void RemoveCacheByPre()
        {

            RemoveCacheByPre(CacheKeyPre);
        }
        public void RemoveCacheByPre(string pre)
        {
            if (pre.IsNotNullAndEmpty())
            {
                CacheHelper.RemoveByStartsWith(pre);
            }
        }

        /// <summary>
        /// 获取单据编号
        /// </summary>
        /// <param name="voucherType">单据类型</param>
        /// <returns></returns>
        public string GetVoucherCode(string  voucherType) {
            try
            {
                var cmd = this.db.Sql("GetVoucherCode")
                    .CommandType(Data.DbCommandTypes.StoredProcedure)
                   .Parameter("voucherType", voucherType, Data.DataTypes.String, size: 20)
                   .ParameterOut("vouchercode", Data.DataTypes.String, 50);
                cmd.Execute();
                string productName = cmd.ParameterValue<string>("vouchercode");

                return productName;
            }
            catch (Exception ex)
            { 
                throw;
            }
           
        }
        /// <summary>
        /// 批量获取单号
        /// </summary>
        /// <param name="voucherType">Type of the voucher.</param>
        /// <returns></returns>
        public VoucherGenModel GetVoucherCode(string voucherType, int count)
        {
            try
            {
                var cmd = this.db.Sql("GetVoucherCodeBatch")
                    .CommandType(Data.DbCommandTypes.StoredProcedure)
                   .Parameter("voucherType", voucherType, Data.DataTypes.String, size: 20)
                   .Parameter("cnt", count);
                var models = cmd.QueryMany<VoucherGenModel>();
                return models.Count == 0 ? null : models[0];
            }
            catch (Exception ex)
            {
                throw;
            }

        }
        /// <summary>
        /// 集团编号
        /// </summary>
        /// <param name="voucherType">Type of the voucher.</param>
        /// <returns></returns>
        public string GetGroupCode()
        {
            try
            {
                var cmd = this.db.Sql("Gen_GroupCode")
                    .CommandType(Data.DbCommandTypes.StoredProcedure)
                   .ParameterOut("group_code", Data.DataTypes.String, 50);
                cmd.Execute();
                string productName = cmd.ParameterValue<string>("group_code");

                return productName;
            }
            catch (Exception ex)
            {
                throw;
            }

        }
    }
}
